// ==UserScript==
// @name         米黄Trintech插件1.0.0
// @namespace    米黄Trintech插件1.0.0
// @include      *://*cadency.trintech.com/Certification/JEPrep/*
// @version      1.0
// @description  让poJE跟飞一样快。
// @author       You
// @match        https://rockwell.cadency.trintech.com/
// @grant        none
// ==/UserScript==


javascript: (function() {

    // 设置 - 颜色，保存移动位置，设置CC，提示框消失时间，填写清空是否提示
    // 可隐藏
    // 辅助功能 - 调整Sheet宽度
    // 自动以提示框，确认框
    // 单元格自动更新时闪一下颜色

    // 

    var mName = '米黄Trintech插件1.0.0';

    // 从localStorage中获得数据

    var companyCode = localStorage.getItem('companyCode');
    companyCode = companyCode ? companyCode : '5003'; // 默认5003

    var documentType = ''; // 根据是否有Reversal Period Box来自动判断
    var documentCurrency = 'USD'; // 根据conpanyCode自动匹配

    var preparerCostCenter = localStorage.getItem('preparerCostCenter');
    preparerCostCenter = preparerCostCenter ? preparerCostCenter : '89031'; // 默认89031

    var longText = '';
    var shortText = longText.substring(0, 16);

    var postingDate = localStorage.getItem('postingDate');
    postingDate = postingDate ? postingDate : '2020/7/31'; // 默认2020/7/31

    var reversalDate = ''; // 根据postingDate自动计算

    var reversalDay = localStorage.getItem('reversalDay');
    reversalDay = reversalDay ? reversalDay : '10'; // reversal日默认是第二个月的10号

    var controlTotal = 'CT 1'; // 默认CT1 不储存
    var ownership = 'GFO'; // 默认BFO 不储存

    var mFontFamily = localStorage.getItem('mFontFamily');
    mFontFamily = mFontFamily ? mFontFamily : 'Microsoft Yahei'; // 'Calibri,Microsoft Yahei'

    var mColor = localStorage.getItem('mColor');
    mColor = mColor ? mColor : '#333333'; // '#005261'

    // 设置相关数据
    var mTop = localStorage.getItem('mTop');
    mTop = mTop ? mTop : '260px'; // 默认顶部距离
    var mRight = localStorage.getItem('mRight');
    mRight = mRight ? mRight : '50px'; // 默认靠右距离
    var mMsgDisappearTime = localStorage.getItem('mMsgDisappearTime');
    mMsgDisappearTime = mMsgDisappearTime ? mMsgDisappearTime : 2; // 默认2秒
    var mWaitTime = mMsgDisappearTime;

    // 错误数量
    var mError = 0;


    var currencyMapping = {
        '0016': 'JPY',
        '0135': 'VND',
        '0347': 'CNY',
        '5021': 'CNY',
        '5002': 'CNY',
        '5000': 'AUD',
        '5003': 'CNY',
        '5004': 'CNY',
        '5006': 'CNY',
        '5007': 'HKD',
        '5008': 'INR',
        '5010': 'KRW',
        '5011': 'MYR',
        '5012': 'NZD',
        '5013': 'PHP',
        '5014': 'USD',
        '5015': 'USD',
        '5016': 'TWD',
        '5017': 'THB',
        '5017': 'USD',
        '5019': 'IDR',
        '5023': 'CNY',
    };

    var ctMapping = {
        'CT 1': 'Less than 1M USD',
        'CT 2': '1M to 2M USD',
        'CT 3': 'More than 2M USD'
    };


    // 各种盒子 - SAP
    var reversalPeriodBox = document.querySelector('#ReversalPeriod'); // Z1独有的盒子

    var companyCodeBox = document.querySelector('#baseheader_d4137e0c-a437-49a6-8b2c-0a0e6b09e3ee');
    var documentTypeBox = document.querySelector('#detailheader_ecde07c7-5d82-4406-a222-a4d0010f574c');
    var documentCurrencyBox = document.querySelector('#detailheader_4f54f35e-c0ff-4a94-83ac-a46e0112442c');
    var preparerCostCenterBox = document.querySelector('#detailheader_52d955e7-ca17-4689-9270-a4f400db31ee');
    var ownershipBox = document.querySelector('#detailheader_377b988a-42a7-4a2e-ad80-a4f700a59a0a'); // readonly 
    var referenceBox = document.querySelector('#detailheader_69234d49-9a22-4e1a-81b9-a4df00f02bc1');
    var adHocJudgmentalBox = document.querySelector('#detailheader_4a1398cf-1004-4929-9f09-a4d001128f5b');

    var postingDateBox = document.querySelector('#baseheader_e954c74e-f1c5-4b45-89fe-a46e01124419'); // SAP和non-SAP共用
    var documentDateBox = document.querySelector('#detailheader_fbd595aa-4570-4d7f-a6df-a4d00112acc6');
    var translationDateBox = document.querySelector('#detailheader_12b345f4-e5fe-40f8-aa43-a4df00efe030');
    var reversalDateBox = document.querySelector('#baseheader_2bd855a7-6d60-41cc-8333-a46e0112442c');
    var controlTotalBox = document.querySelector('#detailheader_d1f641fd-05d3-4ec7-b868-a4fb009d8d84');
    var ctDescriptionBox = document.querySelector('#detailheader_aea737c3-cf63-4f16-9c15-a50f009de668'); // readonly 
    var jeDescriptionBox = document.querySelector('#detailheader_50993ab5-3add-4924-8f8a-a4df00faebb1'); // SAP和non-SAP共用

    // 各种盒子 - NonSAP
    var nonSAPLocationBox = document.querySelector('#detailheader_02fb3f29-8c29-412d-a656-a71b00edf360');
    var entryCurrencyBox = document.querySelector('#detailheader_6ca1d8e1-c1a1-4915-8ea6-a71b00ff1443');
    var documentNoBox = document.querySelector('#detailheader_4e5c54ab-23d4-4304-ae9a-a71c00d73f19');




    // 插件UI
    var prepareArea = document.querySelector('#PrepWrapper');

    // 主界面
    var mBox = document.createElement('div'); // 装所有按钮的大盒子
    prepareArea.appendChild(mBox);
    mBox.style.backgroundColor = '#fff';
    mBox.style.position = 'absolute';
    mBox.style.height = '270px';
    mBox.style.width = '400px';
    mBox.style.right = mRight;
    mBox.style.top = mTop;
    mBox.style.borderRadius = '25px'
    mBox.style.boxShadow = '0px 0px 5px #333'
    mBox.style.zIndex = '9999999';
    mBox.style.fontFamily = mFontFamily;


    // 提示框
    var msgBox = document.createElement('div');
    msgBox.className = 'msg';
    prepareArea.appendChild(msgBox); // 先添加（透明度0）
    msgBox.style.height = '70px'
    msgBox.style.width = 'auto';
    msgBox.style.userSelect = 'none'; // 文字不能选中

    // msgBox.addEventListener('click', function() { msgBox.style.left = '9999px'; })

    var msgIcon = document.createElement('div'); // 错误正确的小圆圈
    msgIcon.className = 'icon iconCorrect';
    msgBox.appendChild(msgIcon);

    var msg = document.createElement('span');
    msgBox.appendChild(msg);
    msg.style.marginLeft = '10px';
    msg.style.marginRight = '50px';
    msg.style.height = '70px';
    msg.style.lineHeight = '70px';


    // 确认框
    var cover = document.createElement('div');
    cover.className = 'cover';
    prepareArea.appendChild(cover);
    cover.style.position = 'absolute';
    cover.style.display = 'none'; // 刚开始消失
    cover.style.top = '0';
    cover.style.left = '0';
    cover.style.height = '1200px';
    cover.style.width = '100%';
    cover.style.backgroundColor = 'black';
    cover.style.opacity = '0';
    cover.style.zIndex = '9999999';

    var confirmBox = document.createElement('div');
    confirmBox.className = 'msg';
    prepareArea.appendChild(confirmBox); //  先添加（透明度0）
    confirmBox.style.height = '120px';
    confirmBox.style.userSelect = 'none'; // 文字不能选中
    // confirmBox.style.width = '600px';  // 随内容变化

    var confirmIcon = document.createElement('div'); // 错误正确的小圆圈
    confirmIcon.className = 'icon iconCorrect';
    confirmBox.appendChild(confirmIcon);

    var confirmMsg = document.createElement('div');
    confirmBox.appendChild(confirmMsg);
    confirmMsg.style.display = 'inline-block';
    confirmMsg.style.marginLeft = '10px';
    confirmMsg.style.marginRight = '100px';
    confirmMsg.style.textAlign = 'center';
    confirmMsg.style.height = '120px';
    confirmMsg.style.lineHeight = '120px';
    // confirmMsg.style.margin = '0 auto';

    var doWhat = ''; // foncirm做什么

    var btnConfirm = document.createElement('div');
    btnConfirm.className = 'mBtn';
    confirmBox.appendChild(btnConfirm);
    btnConfirm.style.right = '90px';
    btnConfirm.innerHTML = '确认';
    btnConfirm.addEventListener('click', function() {
        confirmBox.style.top = (window.innerHeight - confirmBox.offsetHeight) / 2 - 20 + 'px';
        confirmBox.style.opacity = '0';
        cover.style.display = 'none';
        confirmBox.style.display = 'none'
        if (doWhat == '填写') {
            autoFill();
        } else if (doWhat == '清空') {
            autoClear();
        }
    })

    var btnCancel = document.createElement('div');
    btnCancel.className = 'mBtn';
    confirmBox.appendChild(btnCancel);
    btnCancel.style.right = '30px';
    btnCancel.innerHTML = '取消';
    btnCancel.addEventListener('click', function() {
        confirmBox.style.top = (window.innerHeight - confirmBox.offsetHeight) / 2 - 20 + 'px';
        confirmBox.style.opacity = '0';
        cover.style.display = 'none';
        confirmBox.style.display = 'none'
    })


    // 标题框
    var mTitleBox = document.createElement('div');
    mBox.appendChild(mTitleBox);
    mTitleBox.style.backgroundColor = mColor;
    mTitleBox.style.height = '30px';
    mTitleBox.style.width = '100%';
    mTitleBox.style.borderRadius = '25px 25px 1px 1px';
    mTitleBox.style.margin = '0px';
    mTitleBox.style.boxShadow = '0px 1px 8px #333';
    mTitleBox.style.cursor = 'move';

    mTitleBox.addEventListener('mousedown', function(e) {
        var x = e.pageX - mBox.offsetLeft; // 鼠标距离浏览器左边距离 - 盒子距离浏览器左边距离 = 鼠标距离盒子做边框距离（不变）
        var y = e.pageY - mBox.offsetTop; // 同上，距离顶部
        document.addEventListener('mousemove', move);

        function move(e) {
            mBox.style.right = window.innerWidth - (e.pageX - x) - mBox.offsetWidth + 'px';
            mBox.style.top = e.pageY - y + 'px';
        }

        document.addEventListener('mouseup', function() {
            document.removeEventListener('mousemove', move);
        })
    })

    var mLogoBox = document.createElement('div');
    mTitleBox.appendChild(mLogoBox);
    mLogoBox.style.width = '200px';
    mLogoBox.style.height = '30px';
    mLogoBox.style.margin = '0 auto';

    // var mLogo = document.createElement('div');
    // mLogoBox.appendChild(mLogo);
    // mLogo.style.display = 'inline-block';
    // mLogo.style.height = '25px';
    // mLogo.style.lineHeight = '25px';
    // mLogo.style.width = '25px';
    // mLogo.style.textAlign = 'center';
    // mLogo.style.backgroundColor = '#fff';
    // mLogo.style.borderRadius = '15px';
    // mLogo.style.color = mColor;
    // mLogo.innerHTML = 'M';

    var mTitle = document.createElement('h1');
    mLogoBox.appendChild(mTitle);
    mTitle.innerHTML = mName;
    mTitle.style.display = 'inline-block';
    mTitle.style.fontSize = '12px';
    mTitle.style.textAlign = 'center';
    mTitle.style.height = '30px';
    mTitle.style.lineHeight = '30px';
    mTitle.style.width = '100%';
    mTitle.style.fontFamily = mFontFamily;
    mTitle.style.color = '#fff';
    mTitle.style.margin = '0px';
    mTitle.style.userSelect = 'none'; // 文字不能选中

    var mUl = document.createElement('ul');
    mBox.appendChild(mUl);



    // 第一个Li - companyCode
    var mLi1 = document.createElement('li');
    mLi1.className = 'mLi';
    mUl.appendChild(mLi1);

    var mText1 = document.createElement('i'); // 字段CompanyCode
    mText1.className = 'mText';
    mLi1.appendChild(mText1);
    mText1.innerHTML = 'CompanyCode ';
    var mCompanyCodeBox = document.createElement('input'); // 单元格
    mCompanyCodeBox.className = 'mInput';
    mLi1.appendChild(mCompanyCodeBox);
    var mCheckCompanyCode = document.createElement('i');
    mCheckCompanyCode.className = 'mText mCheck';
    mLi1.appendChild(mCheckCompanyCode);

    mCompanyCodeBox.addEventListener('blur', function() {
        companyCode = mCompanyCodeBox.value;
        documentCurrency = mapCurrency(mCompanyCodeBox.value);
        mCurrencyBox.value = documentCurrency;
        checkAll();
    })




    // 第二个Li - SB or Z1
    var mLi2 = document.createElement('li');
    mLi2.className = 'mLi';
    mUl.appendChild(mLi2);

    var mDocumentType1 = document.createElement('input');
    mDocumentType1.type = 'radio';
    mDocumentType1.name = 'DocumentType';
    mDocumentType1.innerHTML = 'SB'
    mDocumentType1.style.color = mColor;
    mLi2.appendChild(mDocumentType1);
    var mText2 = document.createElement('i');
    mText2.className = 'mText';
    mLi2.appendChild(mText2);
    mText2.innerHTML = 'SB ';
    var mDocumentType2 = document.createElement('input');
    mDocumentType2.type = 'radio';
    mDocumentType2.name = 'DocumentType';
    mDocumentType2.innerHTML = 'SB'
    mDocumentType2.style.color = mColor;
    mLi2.appendChild(mDocumentType2);
    var mText3 = document.createElement('i');
    mText3.className = 'mText';
    mLi2.appendChild(mText3);
    mText3.innerHTML = 'Z1 ';

    mDocumentType1.addEventListener('click', function() {
        documentType = 'SB';
        selectDocumentType(documentType);
    });

    mDocumentType2.addEventListener('click', function() {
        documentType = 'Z1';
        selectDocumentType(documentType);
    });



    // 第二点五个 Currency
    var mLi25 = document.createElement('li');
    mLi25.className = 'mLi';
    mUl.appendChild(mLi25);

    var mText25 = document.createElement('i');
    mText25.className = 'mText';
    mLi25.appendChild(mText25);
    mText25.innerHTML = 'Currency ';
    var mCurrencyBox = document.createElement('input');
    mCurrencyBox.className = 'mInput';
    mLi25.appendChild(mCurrencyBox);
    var mCheckCurrency = document.createElement('i');
    mCheckCurrency.className = 'mText mCheck';
    mLi25.appendChild(mCheckCurrency);

    mCurrencyBox.addEventListener('blur', function() {
        documentCurrency = mCurrencyBox.value;
        checkAll();
    })






    // 第三个Date
    var mLi3 = document.createElement('li');
    mLi3.className = 'mLi';
    mUl.appendChild(mLi3);

    var mText33 = document.createElement('i');
    mText33.className = 'mText';
    mLi3.appendChild(mText33);
    mText33.innerHTML = 'PostingDate (Y/M/D) ';
    var mPostingDateBox = document.createElement('input');
    mPostingDateBox.className = 'mInput';
    mLi3.appendChild(mPostingDateBox);
    var mCheckPostingDate = document.createElement('i');
    mCheckPostingDate.className = 'mText mCheck';
    mLi3.appendChild(mCheckPostingDate);

    var mLi4 = document.createElement('li');
    mLi4.className = 'mLi';
    mUl.appendChild(mLi4);

    var mText4 = document.createElement('i');
    mText4.className = 'mText';
    mLi4.appendChild(mText4);
    mText4.innerHTML = 'ReversalDate ';
    var mReversalDateBox = document.createElement('input');
    mReversalDateBox.className = 'mInput';
    mLi4.appendChild(mReversalDateBox);
    var mCheckReversalDate = document.createElement('i');
    mCheckReversalDate.className = 'mText mCheck';
    mLi4.appendChild(mCheckReversalDate);

    mPostingDateBox.addEventListener('blur', function() {
        // 修改posting日期后自动保存
        postingDate = mPostingDateBox.value;
        selectDocumentType(documentType);
        checkAll();
    })

    mReversalDateBox.addEventListener('blur', function() {
        reversalDate = mReversalDateBox.value;
        checkAll();
    })



    // 第四个LongText
    var mLi5 = document.createElement('li');
    mLi5.className = 'mLi';
    mUl.appendChild(mLi5);

    var mText5 = document.createElement('i');
    mText5.className = 'mText';
    mLi5.appendChild(mText5);
    mText5.innerHTML = 'LongText ';
    var mLongTextBox = document.createElement('input');
    mLi5.appendChild(mLongTextBox);
    mLongTextBox.style.width = '200px';
    // mLongTextBox.autocomplete = 'false';
    mLongTextBox.setAttribute('autocomplete', 'off')
    mLongTextBox.setAttribute('placeholder', '在这里输入JE Description')
    mLongTextBox.style.float = 'right';
    mLongTextBox.style.marginRight = '50px';
    mLongTextBox.style.fontFamily = mFontFamily;


    // 下边的按钮
    var btnFill = document.createElement('div');
    btnFill.className = 'mBtn';
    mBox.appendChild(btnFill);
    btnFill.style.left = '40px';
    btnFill.innerHTML = '填写';
    btnFill.addEventListener('click', function() {
        // autoFill();
        if (mError == 0) {
            var system = companyCodeBox ? 'SAP' : 'non-SAP';
            showConfirm('检测到当前的Template是：' + system + '，是否自动填写JE？', '填写');
        } else {
            showMsg('不能填写：有' + mError + '处错误。', false, function() {});
        }
    })

    var btnClear = document.createElement('div');
    btnClear.className = 'mBtn';
    mBox.appendChild(btnClear);
    btnClear.style.left = (40 + (50 + 10) * (2 - 1)) + 'px';
    btnClear.innerHTML = '清空';
    btnClear.addEventListener('click', function() {
        var system = companyCodeBox ? 'SAP' : 'non-SAP';
        showConfirm('检测到当前的Template是：' + system + '，是否清空所有内容？', '清空');
        // autoClear();
    })

    var btnReset = document.createElement('div');
    btnReset.className = 'mBtn';
    mBox.appendChild(btnReset);
    btnReset.style.left = (40 + (50 + 10) * (3 - 1)) + 'px';
    btnReset.innerHTML = '重置';
    btnReset.addEventListener('click', function() {
        autoReset();
    })




    // 填写默认值
    // 填写默认值
    // 填写默认值
    // 填写默认值
    // 填写默认值
    mCompanyCodeBox.value = companyCode;
    documentType = reversalPeriodBox ? 'Z1' : 'SB';
    if (documentType == 'SB') {
        mDocumentType1.setAttribute('checked', 'checked');
        mDocumentType2.disabled = true;
    } else {
        mDocumentType2.setAttribute('checked', 'checked');
        mDocumentType1.disabled = true;
    };
    selectDocumentType(documentType);
    mCurrencyBox.value = mapCurrency(companyCode);
    mPostingDateBox.value = postingDate;
    mReversalDateBox.value = reversalDate;
    mLongTextBox.value = longText;
    checkAll(); // 检测







    // 统一调整格式
    var mStyleFori = document.getElementsByClassName('mText');
    for (var i = 0; i < mStyleFori.length; i++) {
        mStyleFori[i].style.fontStyle = 'normal';
        mStyleFori[i].style.height = '20px';
        mStyleFori[i].style.lineHeight = '20px';
    }
    var mStyleForli = document.getElementsByClassName('mLi');
    for (var i1 = 0; i1 < mStyleForli.length; i1++) {
        mStyleForli[i1].style.position = 'relative';
        mStyleForli[i1].style.listStyle = 'none';
        mStyleForli[i1].style.marginTop = '10px';
    }
    var mStyleForInput = document.getElementsByClassName('mInput');
    for (var i2 = 0; i2 < mStyleForInput.length; i2++) {
        mStyleForInput[i2].style.width = '100px';
        // mStyleForInput[i2].style.autocomplete = 'false';
        mStyleForInput[i2].setAttribute('autocomplete', 'off');
        mStyleForInput[i2].style.float = 'right';
        mStyleForInput[i2].style.marginRight = '50px';
        mStyleForInput[i2].style.fontFamily = mFontFamily;
        mStyleForInput[i2].style.fontSize = '14px';
    }
    var mStyleForBtn = document.getElementsByClassName('mBtn');
    for (var i3 = 0; i3 < mStyleForBtn.length; i3++) {
        mStyleForBtn[i3].style.backgroundColor = '#fff'; // 淡色 #7fa8b0
        mStyleForBtn[i3].style.position = 'absolute';
        mStyleForBtn[i3].style.height = '50px';
        mStyleForBtn[i3].style.width = '50px';
        // mStyleForBtn[i3].style.left = '40px';
        mStyleForBtn[i3].style.top = '235px';
        mStyleForBtn[i3].style.borderRadius = '25px';
        mStyleForBtn[i3].style.boxShadow = '0px 0px 5px #333';
        mStyleForBtn[i3].style.cursor = 'pointer';
        mStyleForBtn[i3].style.zIndex = '9999999';
        // mStyleForBtn[i3].innerHTML = '填写';
        mStyleForBtn[i3].style.textAlign = 'center';
        mStyleForBtn[i3].style.color = '#333'; // 灰色
        mStyleForBtn[i3].style.lineHeight = '50px'; // 灰色
        mStyleForBtn[i3].style.userSelect = 'none'; // 不能选中按钮里面的文字

        mStyleForBtn[i3].addEventListener('mouseenter', function() {
            this.style.color = '#fff';
            this.style.backgroundColor = '#54565a'; // 灰色
            this.style.border = 'none';
        })
        mStyleForBtn[i3].addEventListener('mouseleave', function() {
            this.style.backgroundColor = '#fff';
            this.style.color = '#333';
        })
        mStyleForBtn[i3].addEventListener('mousedown', function() {
            this.style.backgroundColor = mColor; // 深色
        })
        mStyleForBtn[i3].addEventListener('mouseup', function() {
            this.style.backgroundColor = '#54565a'; // 灰色
        })
    }
    var mStyleForCheck = document.getElementsByClassName('mCheck');
    for (var i4 = 0; i4 < mStyleForCheck.length; i4++) {
        mStyleForCheck[i4].style.position = 'absolute';
        mStyleForCheck[i4].style.top = '5px';
        mStyleForCheck[i4].style.right = '30px';
        mStyleForCheck[i4].style.fontSize = '10px';
        mStyleForCheck[i4].style.height = '15px';
        mStyleForCheck[i4].style.width = '15px';
        mStyleForCheck[i4].style.lineHeight = '15px';
        mStyleForCheck[i4].style.textAlign = 'center';
        mStyleForCheck[i4].style.fontWeight = '700';

    }
    var mStyleForCheckCorrect = document.getElementsByClassName('mCorrect');
    for (var i5 = 0; i5 < mStyleForCheckCorrect.length; i5++) {
        mStyleForCheckCorrect[i5].style.backgroundColor = '#99cc66';
        mStyleForCheckCorrect[i5].style.borderRadius = '20px';
        mStyleForCheckCorrect[i5].style.color = '#fff';
        mStyleForCheckCorrect[i5].innerHTML = '√';
        mStyleForCheckCorrect[i5].style.mFontFamily = 'Candara';
        mStyleForCheckCorrect[i5].style.cursor = 'pointer';
        mStyleForCheckCorrect[i5].style.userSelect = 'none'; // 不能选中按钮里面的文字
    }
    var mStyleForCheckError = document.getElementsByClassName('mError');
    for (var i6 = 0; i6 < mStyleForCheckError.length; i6++) {
        mStyleForCheckError[i6].style.backgroundColor = '#cc0001';
        mStyleForCheckError[i6].style.borderRadius = '20px';
        mStyleForCheckError[i6].style.color = '#fff';
        mStyleForCheckError[i6].style.mFontFamily = 'Candara';
        mStyleForCheckError[i6].innerHTML = '×';
        mStyleForCheckError[i6].style.cursor = 'pointer';
        mStyleForCheckError[i6].style.userSelect = 'none'; // 不能选中按钮里面的文字
    }
    var mStyleForMsg = document.getElementsByClassName('msg');
    for (var i7 = 0; i7 < mStyleForMsg.length; i7++) {
        mStyleForMsg[i7].style.display = 'none'; // 开启时先隐藏
        mStyleForMsg[i7].style.display = 'inline-block';
        mStyleForMsg[i7].style.position = 'absolute';
        // mStyleForMsg[i7].style.height = '70px';
        // mStyleForMsg[i7].style.width = 'auto'; // 宽度随内容变化而变化
        mStyleForMsg[i7].style.top = (window.innerHeight - 70) / 2 + 'px';
        mStyleForMsg[i7].style.left = (window.innerWidth - mStyleForMsg[i7].offsetWidth) / 2 + 'px';
        mStyleForMsg[i7].style.backgroundColor = '#fff';
        mStyleForMsg[i7].style.borderRadius = '25px'
        mStyleForMsg[i7].style.boxShadow = '0px 0px 5px #333'
        mStyleForMsg[i7].style.zIndex = '9999999';
        mStyleForMsg[i7].style.fontFamily = mFontFamily;
        mStyleForMsg[i7].style.opacity = '0'; // 开始透明
    }
    var mStyleForIcon = document.getElementsByClassName('icon');
    for (var i8 = 0; i8 < mStyleForIcon.length; i8++) {
        mStyleForIcon[i8].style.display = 'inline-block';
        mStyleForIcon[i8].style.height = '30px';
        mStyleForIcon[i8].style.width = '30px';
        mStyleForIcon[i8].style.lineHeight = '30px';
        mStyleForIcon[i8].style.textAlign = 'center';
        mStyleForIcon[i8].style.color = '#fff';
        mStyleForIcon[i8].style.fontWeight = '700';
        mStyleForIcon[i8].style.borderRadius = '15px'
        mStyleForIcon[i8].style.mFontFamily = 'Candara';
        mStyleForIcon[i8].style.marginLeft = '50px';
    }
    var mStyleForIconCorrect = document.getElementsByClassName('iconCorrect');
    for (var i9 = 0; i9 < mStyleForIconCorrect.length; i9++) {
        mStyleForIconCorrect[i9].style.backgroundColor = '#99cc66';
        mStyleForIconCorrect[i9].innerHTML = '√'
    }
    var mStyleForIconError = document.getElementsByClassName('iconError');
    for (var i10 = 0; i10 < mStyleForIconError.length; i10++) {
        mStyleForIconError[i10].style.backgroundColor = '#cc0001';
        mStyleForIconError[i10].innerHTML = '×'
    }









    // 下边是函数
    // 下边是函数
    // 下边是函数
    // 下边是函数
    // 下边是函数
    function autoFill(cfmAutoFill) {
        var system = companyCodeBox ? 'SAP' : 'non-SAP';

        cfmAutoFill = true;
        // if (mError == 0) {
        //     var cfmAutoFill = showConfirm('检测到当前的Template是：' + system + '，是否自动填写JE？', '填写');
        //     // var cfmAutoFill = confirm('检测到当前的Template是：' + system + '，是否自动填写JE？');
        // } else {
        //     showMsg('不能填写：有' + mError + '处错误。', false, function() {
        //         // msgBox.style.display = 'none';
        //     })
        // }

        if (cfmAutoFill == true && system == 'SAP') {

            companyCode = mCompanyCodeBox.value;
            if (mDocumentType1.checked) {
                documentType = 'SB';
                reversalDate = '';
            } else {
                documentType = 'Z1';
                reversalDate = mReversalDateBox.value;
            };
            postingDate = mPostingDateBox.value;
            longText = mLongTextBox.value;
            shortText = longText.substring(0, 16);

            companyCodeBox.value = companyCode;
            documentTypeBox.value = documentType;
            documentCurrencyBox.value = documentCurrency;
            preparerCostCenterBox.value = preparerCostCenter;
            referenceBox.value = shortText;
            adHocJudgmentalBox.value = shortText;

            postingDateBox.value = postingDate;
            documentDateBox.value = postingDate;
            translationDateBox.value = postingDate;
            reversalDateBox.value = reversalDate;
            controlTotalBox.value = controlTotal;
            jeDescriptionBox.value = longText;

            ctDescriptionBox.setAttribute('readonly', ''); // 填写CT
            ctDescriptionBox.value = ctMapping[controlTotal];
            ctDescriptionBox.setAttribute('readonly', 'readonly');;

            ownershipBox.setAttribute('readonly', ''); // 填写ownership
            ownershipBox.value = ownership;
            ownershipBox.setAttribute('readonly', 'readonly');

            // 把当前数据保存到localStorage
            localStorage.setItem('companyCode', companyCode)
            localStorage.setItem('preparerCostCenter', preparerCostCenter)
            localStorage.setItem('postingDate', postingDate)

        } else if (cfmAutoFill == true && system != 'SAP') {
            companyCode = mCompanyCodeBox.value; // 这里就是nonSAP Location
            postingDate = mPostingDateBox.value;
            longText = mLongTextBox.value;
            shortText = longText.substring(0, 14); // 这里就是Document No - 14个字符

            nonSAPLocationBox.value = companyCode;
            postingDateBox.value = postingDate;
            entryCurrencyBox.value = documentCurrency;
            jeDescriptionBox.value = longText;
            documentNoBox.value = shortText;

            // 把当前数据保存到localStorage
            localStorage.setItem('companyCode', companyCode)
            localStorage.setItem('preparerCostCenter', preparerCostCenter)
            localStorage.setItem('postingDate', postingDate)
        }
    }


    function autoClear() {
        var system = companyCodeBox ? 'SAP' : 'non-SAP';
        // var cfmAutoClear = confirm('检测到当前的Template是：' + system + '，是否清空所有内容？');
        var cfmAutoClear = true
        if (cfmAutoClear == true && system == 'SAP') {
            companyCodeBox.value = '';
            documentTypeBox.value = '';
            documentCurrencyBox.value = '';
            preparerCostCenterBox.value = '';
            referenceBox.value = '';
            adHocJudgmentalBox.value = '';

            postingDateBox.value = '';
            documentDateBox.value = '';
            translationDateBox.value = '';
            reversalDateBox.value = '';
            controlTotalBox.value = '';
            jeDescriptionBox.value = '';
        } else if (cfmAutoClear == true && system != 'SAP') {
            nonSAPLocationBox.value = '';
            postingDateBox.value = '';
            entryCurrencyBox.value = '';
            jeDescriptionBox.value = '';
            documentNoBox.value = '';
        }
    }



    // 选择SB还是Z1
    function selectDocumentType(whichType) {
        if (whichType == 'SB') {
            mReversalDateBox.value = '';
            mReversalDateBox.disabled = 'true';
        } else {
            var date = postingDate.split('/');
            var reversalYear = 0;
            var reversalMonth = 0;
            var postingYear = parseInt(date[0]);
            var postingMonth = parseInt(date[1]);
            if (postingMonth >= 12) {
                reversalMonth = 1;
                reversalYear = postingYear + 1;
            } else {
                reversalMonth = postingMonth + 1;
                reversalYear = postingYear;
            }

            reversalDate = reversalYear + '/' + reversalMonth + '/' + reversalDay;

            mReversalDateBox.value = reversalDate;
            mReversalDateBox.disabled = '';
        }
        checkAll();
    }



    // 获取国家币种 - 如果没有就返回USD
    function mapCurrency(whichContry) {
        if (currencyMapping[whichContry] == undefined) {
            return 'USD';
        } else {
            return currencyMapping[whichContry];
        }
    }


    // 设置对错Style
    function setCheckCSS(checkObject) {
        if (checkObject.className.search('mCorrect') != -1) {
            checkObject.style.backgroundColor = '#99cc66';
            checkObject.innerHTML = '√';
        } else {
            checkObject.style.backgroundColor = '#cc0001';
            checkObject.innerHTML = '×';
        }
        checkObject.style.borderRadius = '20px';
        checkObject.style.color = '#fff';
        checkObject.style.mFontFamily = 'Candara';
        checkObject.style.display = 'block';
    }


    // Check所有的项目
    function checkAll() {
        mError = 0; // 初始化

        // CompanyCode 
        var mCurr = currencyMapping[companyCode];
        var companyCodeExsit = mCurr === undefined ? false : true; // 看国家是否存在 - 根据是否能匹配到货币
        mCheckCompanyCode.className = companyCodeExsit ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (!companyCodeExsit) { mError += 1 };
        setCheckCSS(mCheckCompanyCode);
        // Currency
        var mCurry = mCurrencyBox.value;
        mCheckCurrency.className = mCurry.length == 3 ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (mCurry.length != 3) { mError += 1 };
        setCheckCSS(mCheckCurrency);
        // PostingDate 验证是否日期格式
        var d = new Date(postingDate);
        var isDate = (d.getDate() == postingDate.substring(postingDate.length - 2));
        mCheckPostingDate.className = isDate ? 'mText mCheck mCorrect' : 'mText mCheck mError';
        if (!isDate) { mError += 1 };
        setCheckCSS(mCheckPostingDate);
        // ReversalDate 验证是否日期格式
        if (documentType != 'SB') {
            var dd = new Date(reversalDate);
            var isDate2 = (dd.getDate() == reversalDate.substring(reversalDate.length - 2));
            mCheckReversalDate.className = isDate2 ? 'mText mCheck mCorrect' : 'mText mCheck mError';
            if (!isDate2) { mError += 1 };
            setCheckCSS(mCheckReversalDate);
        } else {
            mCheckReversalDate.className = 'mText mCheck';
            mCheckReversalDate.style.display = 'none';
        }
    }


    // 重置面板
    function autoReset() {
        companyCode = localStorage.getItem('companyCode');
        companyCode = companyCode ? companyCode : '5003';
        mapCurrency(companyCode);
        postingDate = localStorage.getItem('postingDate');
        postingDate = postingDate ? postingDate : '2020/7/31';
        selectDocumentType(documentType);
        mCompanyCodeBox.value = companyCode;
        mCurrencyBox.value = mapCurrency(companyCode);
        mPostingDateBox.value = postingDate;
        mReversalDateBox.value = reversalDate;
        checkAll();
        showMsg('重置完成。', true, function() {
            // msgBox.style.display = 'none';
            // msgBox.style.top = (window.innerHeight - 70) / 2 + 20 + 'px';
        });
    }



    function showMsg(message, isOK, callback) {
        clearInterval(msgBox.timer);
        msgBox.style.transition = 'opacity 0.5s,top 0.5s';
        msgBox.style.opacity = '0';
        msgBox.style.top = (window.innerHeight - 70) / 2 - 20 + 'px';
        if (isOK) {
            msgIcon.className = 'icon iconCorrect';
            msgIcon.style.backgroundColor = '#99cc66';
            msgIcon.innerHTML = '√'
        } else {
            msgIcon.className = 'icon iconError';
            msgIcon.style.backgroundColor = '#cc0001';
            msgIcon.innerHTML = '×'
        }
        msg.innerHTML = message;
        msgBox.style.position = 'fixed';
        msgBox.style.display = 'block';
        msgBox.style.left = (window.innerWidth - msgBox.offsetWidth) / 2 + 'px';
        msgBox.style.top = (window.innerHeight - 70) / 2 + 'px';
        msgBox.style.opacity = '1'; // 透明度变1

        clearInterval(msgBox.timer); // 先清除定时器，只能有一个定时器执行。
        msgBox.timer = setInterval(function() {
            if (mWaitTime > 1) {
                mWaitTime--;
            } else {
                msgBox.style.top = (window.innerHeight - 70) / 2 - 20 + 'px';
                msgBox.style.opacity = '0';
                clearInterval(msgBox.timer);
                mWaitTime = mMsgDisappearTime; // 初始化
                if (callback) {
                    callback(); // 回调函数
                };
            }
        }, 1000);
    }



    function showConfirm(message, whatToDo) {
        confirmBox.style.position = 'fixed';
        confirmBox.style.display = 'block';
        confirmMsg.innerHTML = message;
        confirmBox.style.top = (window.innerHeight - confirmBox.offsetHeight) / 2 - 20 + 'px';
        confirmBox.style.left = (window.innerWidth - confirmBox.offsetWidth) / 2 + 'px';
        confirmIcon.style.marginLeft = '100px';
        confirmIcon.style.backgroundColor = mColor;
        confirmIcon.innerHTML = '?';
        btnConfirm.style.top = '90px';
        btnCancel.style.top = '90px';
        confirmBox.style.transition = 'opacity 0.5s,top 0.5s';
        confirmBox.style.opacity = '1';
        confirmBox.style.top = (window.innerHeight - confirmBox.offsetHeight) / 2 + 'px';
        cover.style.display = 'block';
        cover.style.transition = 'opacity 0.5s';
        cover.style.opacity = '.3';

        doWhat = whatToDo;
    }

})();